#!/usr/bin/env python
# coding: utf-8

# from pandas import *  python社区禁止的操作方式，函数/变量过多造成冲突
import pandas as pd
import numpy as np

# 声明Series对象
s = pd.Series([1, 3, -23, 90])
s
s_1 = pd.Series([1, 3, 6, 7, -8, 109], index = ['a', 'b', 'c', 'd', 'f', 'g'])
s_1
s_1.index
s_1.values

# 选择内部元素
s_1[2]
s_1['f']
s_1[1:4]
s_1[['b','f']]

# 为元素赋值
s_1['f'] = -9999
s_1

# 用Numpy数组/其他Series对象定义新的Series对象
arr = np.array([1, 3, 5, 19, 200])
s2 = pd.Series(arr)
s2
s3 = pd.Series(s2)
s3
arr[1] = 100
s3

# 筛选元素
s3 > 5
s3[s3 > 5]

# Series对象运算和数学函数
s3 / 2
np.log(s3)

# Series对象的组成元素
serd = pd.Series([1, 0, 3, 2, 1, 2, 9], index = ['blue', 'green', 'yello', 'white', 'black', 'green', 'yello'])
serd
serd.unique()

# 每个元素出现的次数
serd.value_counts()
serd.isin([2,1])
serd[serd.isin([2, 1])]

# NaN(Not a Number)
s6 = pd.Series([5, -3, np.nan, 14])
s6
s6.isnull()
s6.notnull()
s6[s6.isnull()]
s6[s6.notnull()]

# Series用作字典(dict)
mydict = {'red':2000, 'blue':'1000', 'yellow':500, 'orange':1000}
myseries = pd.Series(mydict)
myseries
colors = ['red', 'orange', 'yellow', 'green', 'light blue', 'blue', 'purple']
myseries = pd.Series(mydict, index = colors)
myseries
mydict2 = {'red':400, 'yellow':1000, 'black':700}
myseries2 = pd.Series(mydict2)
    # “+”运算时，任何一个Series中没有值，结果都为NaN
myseries + myseries2